<template>
  <div>
    <el-dialog
      class="base-dialog"
      :class="{ readonly: readOnly }"
      :custom-class="'panna-dialog_big'"
      :title="title"
      align="left"
      :append-to-body="true"
      :visible.sync="innerDialogFormVisible"
      :before-close="handleClose"
    >
      <p slot="title">{{ title }}</p>
      <div v-if="systemType == 'cdc'">
        <span>批量设置接收时间：</span>
        <el-date-picker
          v-model="receivingDate"
          style="width: 200px"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        >
        </el-date-picker>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
      <el-table
        ref="table"
        v-loading="tableLoad"
        :data="tableData"
        height="400px"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column v-if="systemType == 'cdc'" type="selection" width="55">
        </el-table-column>
        <el-table-column prop="sampleNum" label="样品编号"></el-table-column>
        <el-table-column prop="orderNum" label="委托单号"></el-table-column>
        <el-table-column prop="sampleName" label="样品名称"></el-table-column>
        <el-table-column prop="detectionItemName" label="检测项目" />
        <el-table-column
          v-if="systemType == 'cdc'"
          prop="samplingDateTime"
          label="采样时间"
        />
        <el-table-column prop="sampleCount" label="样品接收数量">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.sampleCount"
              :disabled="readOnly"
              autocomplete="off"
              oninput="value=value.replace(/[^\d]/g,'')"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column
          v-if="readOnly"
          prop="receivingPerson"
          label="接收人"
        />
        <el-table-column
          v-if="systemType == 'cdc'"
          :disabled="readOnly"
          prop="receivingDate"
          label="接收时间"
          width="170px"
        >
          <template slot-scope="scope">
            <el-date-picker
              v-model="scope.row.receivingDate"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              placeholder="选择日期"
            >
            </el-date-picker>
          </template>
        </el-table-column>
      </el-table>
      <span v-if="!readOnly" slot="footer" class="base-dialog-footer">
        <el-button
          type="primary"
          :disabled="!submitReadOnly"
          @click="onSubmit('formData')"
          >保 存</el-button
        >
        <el-button @click="handleClose">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import _ from "lodash";
import { creates } from "../../../api/SampleManage/SampleReceiving";
export default {
  name: "SampleReceivingDialog",
  components: {},
  props: {
    systemType: {
      type: String,
      default: "",
    },
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
    editId: {
      type: String,
      default: "",
    },
    readOnly: {
      type: Boolean,
      default: false,
    },
    // 接收样品数据
    rowData: {
      type: Array,
      default: function() {
        return [];
      },
    },
  },
  data() {
    return {
      title: "",
      tableLoad: false,
      tableData: [],
      tableDatas: [],
      submitLoad: false,
      submitReadOnly: true,
      innerDialogFormVisible: false,
      receivingDate: null,
    };
  },
  watch: {
    rowData(val) {
      this.tableData = val;
      for (let i = 0; i < this.tableData.length; i++) {
        if (
          this.tableData[i].sampleCount === "" ||
          this.tableData[i].sampleCount === null ||
          this.tableData[i].sampleCount === 0
        ) {
          this.tableData[i].sampleCount = 1;
        }
      }
    },
    dialogFormVisible(val) {
      this.innerDialogFormVisible = val;
      this.title = this.readOnly
        ? "查看样品接收信息"
        : this.editId
          ? "查看样品接收信息"
          : "样品接收";
    },
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  created() {},
  methods: {
    // 确定
    confirm() {
      for (let i = 0; i < this.tableDatas.length; i++) {
        for (let o = 0; o < this.tableData.length; o++) {
          if (String(this.tableDatas[i].id) === String(this.tableData[o].id)) {
            this.tableData[o].receivingDate = this.receivingDate;
          }
        }
      }
    },
    // 多选
    handleSelectionChange(val) {
      this.tableDatas = val;
    },
    // 提交
    onSubmit: _.throttle(function() {
      if (this.submitReadOnly) {
        this.submitReadOnly = false;
        var data = [];
        for (let i = 0; i < this.tableData.length; i++) {
          if (this.tableData[i].sampleCount <= 0) {
            this.$message.warning("接收数量不能小于等于0");
            this.submitReadOnly = true;
            return false;
          }
          if (this.systemType === "cdc") {
            if (
              this.tableData[i].receivingDate === null ||
              this.tableData[i].receivingDate === "" ||
              this.tableData[i].receivingDate === undefined
            ) {
              this.$message.warning("接收日期不能为空");
              this.submitReadOnly = true;
              return false;
            }
          }

          data.push({
            SampleRegistrationId: this.tableData[i].id,
            EntrustOrderId: this.tableData[i].entrustOrderId,
            SysBusinessSampleInfoId: this.tableData[i].sysBusinessSampleInfoId,
            SampleCount: this.tableData[i].sampleCount,
            ReceivingPerson: this.$store.getters.currentUserId,
            ReceivingDate: this.tableData[i].receivingDate,
            orderNum:
              this.tableData[i].orderNum != null
                ? this.tableData[i].orderNum
                : "",
          });
        }
        if (!this.editId) {
          this.addAPI(data);
        } else {
          this.editAPI();
        }
        return false;
      }
    }, 2000),
    // 新增
    addAPI(data) {
      creates(data)
        .then((res) => {
          this.submitReadOnly = true;
          this.handleClose();
        })
        .catch(() => {
          this.submitReadOnly = true;
        });
    },
    // 关闭弹框
    handleClose() {
      this.tableDatas = [];
      this.receivingDate = null;
      this.closeDialog();
    },
    cancel() {
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      // 清空数据
      this.$emit("closeSampleReceivingDialog");
      this.tableData = [];
      this.cancel();
    },
  },
};
</script>
